package pikes.ecma;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

public class SourceElements extends PrintableSourceElement implements Collection<SourceElement> {

	private LinkedList<SourceElement> sourceElementList = new LinkedList<SourceElement>();

	public final boolean add(SourceElement sourceElement) {
		if (sourceElement != null) {
			return sourceElementList.add(sourceElement);
		}
		return false;
	}

	public final void add(IExpression expression) {
		add(new ExpressionStatement(expression));
	}

	public final void visit(SyntaxTreeVisitor syntaxVisitor) {
		for (SourceElement sourceElement : sourceElementList) {
			syntaxVisitor.beforeSourceElement();
			sourceElement.visit(syntaxVisitor);
		}
	}

	public final boolean isEmpty() {
		return sourceElementList.isEmpty();
	}

	@Override
	public final boolean equals(Object obj) {
		if (!(obj instanceof SourceElements)) {
			return false;
		}

		SourceElements thatSourceElements = (SourceElements) obj;

		return sourceElementList.equals(thatSourceElements.sourceElementList);
	}

	@Override
	public final int hashCode() {

		int hash = 0;

		for (SourceElement sourceElement : sourceElementList) {
			hash = hash + sourceElement.hashCode();
		}

		return hash;
	}

	public final boolean addAll(Collection<? extends SourceElement> c) {
		return sourceElementList.addAll(c);
	}

	public final void clear() {
		sourceElementList.clear();
	}

	public final boolean contains(Object o) {
		return sourceElementList.contains(o);
	}

	public final boolean containsAll(Collection<?> c) {
		return sourceElementList.containsAll(c);
	}

	public final Iterator<SourceElement> iterator() {
		return sourceElementList.iterator();
	}

	public final boolean remove(Object o) {
		return sourceElementList.remove(o);
	}

	public final boolean removeAll(Collection<?> c) {
		return sourceElementList.removeAll(c);
	}

	public final boolean retainAll(Collection<?> c) {
		return sourceElementList.retainAll(c);
	}

	public final int size() {
		return sourceElementList.size();
	}

	public final Object[] toArray() {
		return sourceElementList.toArray();
	}

	public final <T> T[] toArray(T[] a) {
		return sourceElementList.toArray(a);
	}
}
